#include <bits/stdc++.h>
using namespace std;
const int N = 10010;
int n;

int main() {
	priority_queue<int, vector<int>, greater<int>> q;
	cin >> n;
	for (int i = 0; i < n; ++i) {
		int a;
		cin >> a;
		q.push(a);
	}
	long long res = 0;
	while (q.size() > 1) {
		auto a = q.top();
		q.pop();
		auto b = q.top();
		q.pop();
		res += a + b;
		q.push(a + b);
	}
	cout << res << endl;
	return 0;
}